//
//  RuningViewController.m
//  SeaChat
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "RuningViewController.h"

@interface RuningViewController ()<UIScrollViewDelegate,UIAlertViewDelegate,CLLocationManagerDelegate>



{
    
    PQdiskView *view1;
    
    UIButton *starButton;
    UIButton *stopButton;
    UIButton *RunButton;
    
    //    // 计步器实例化对象
    //    CMStepCounter *stepCounter;
    //    // 计步器走的步数；
    int i;
    
    int v;
    
    UIScrollView  *indicators;
    
    
    NSMutableArray *arry1;
    
    // 剪切的当前时间；
    
    NSString *rday;
    
    float k;
    
}
@property(nonatomic,strong)CMPedometer *Pedometer;
@property(nonatomic,strong)CLLocationManager *CLLMan;
@property(nonatomic,strong)CLLocation *oldLocation;
@property(nonatomic,assign)float sum;
@property(nonatomic,assign)NSInteger time_min;
@property(nonatomic,assign)NSInteger time_sec;
@property(nonatomic,assign)NSInteger time_hour;

@property(nonnull,strong)NSTimer *timer;
@property(nonatomic,assign)CGFloat sumkcal;
@property (nonatomic, strong) NSString *month;

@property (nonatomic, assign) NSInteger Sum_counts;

@property (nonatomic , assign) CGFloat sumTime;
//用做gps
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSString *a;
@property (nonatomic, strong) NSString *b;



//需要承载数据的label
@property(nonatomic,strong)UILabel *speedNowLabel;//速度
@property(nonatomic,strong)UILabel *kaluNowLabel;//卡路里
@property(nonatomic,strong)UILabel *timeNowLabel;//时间
@property(nonatomic,strong)UILabel *longDataLabel;//本次跑步距离
@property(nonatomic,strong)UILabel *longAllDataLabel;//本月跑步距离
@property(nonatomic,strong)UIButton *backButton;


@end

@implementation RuningViewController
#pragma mark 跑步需要准备的 1.引入#import <CoreLocation/CoreLocation.h> 2.引入类库 corelocation.framework 3.在info.pilst 加入NSLocationWhenInUseUsageDescription(使用应用程序期间允许访问位置数据) NSLocationAlwaysUsageDescription(始终允许访问位置信息) 后边改为YES
#pragma mark 定位需要准备 1.#import <CoreMotion/CoreMotion.h> 切记CMPedometer 设置成全局变量 否则将会提前被释放


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.dataArr = [NSMutableArray array];
    [self startInster];
    
    [self runingLong];

}


#pragma mark 布局
-(void)startInster{
    [[DataBaseManeger sharaInstance] openDB];
    
    self.dataArr = [[DataBaseManeger sharaInstance] selectAll];
    //    NSLog(@"数据库数组%@",self.dataArr);
    
    
    indicators =  [[UIScrollView alloc] init];
    indicators.delegate = self;
    [self.view addSubview:indicators];
    indicators.backgroundColor = RGBA(240, 255, 255, 1);
    [indicators mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.left.equalTo(self.view.mas_left).offset(0);
        make.width.equalTo(@([UIScreen mainScreen].bounds.size.width));
        make.height.equalTo(@([UIScreen mainScreen].bounds.size.height));
    }];
    indicators.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    view1  =[[PQdiskView alloc]initWithFrame:CGRectMake(0,0, APPW,APPW-30)];
    view1.backgroundColor = RGBA(240, 255, 255, 1);
    view1.num = v;
    [indicators addSubview:view1];
    
#pragma mark 返回按钮
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_backButton];
    self.backButton.backgroundColor = [UIColor clearColor];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(view1).offset(-10);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(35);
        make.top.mas_equalTo(20);
    }];
    [self.backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.backButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
//速度模块
    UILabel *speedLabel = [[UILabel alloc]init];
    speedLabel.backgroundColor = RGBA(237, 237, 237, 1);
    [indicators addSubview:speedLabel];
    speedLabel.text = @"实时速度";
    speedLabel.textAlignment = NSTextAlignmentCenter;
    speedLabel.layer.borderWidth = 1;
    [speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(indicators);
        make.size.mas_equalTo(CGSizeMake(APPW / 3, 30));
        make.top.mas_equalTo(view1.mas_bottom);
        
    }];
//卡路里
    UILabel *kaluLabel = [[UILabel alloc]init];
    kaluLabel.backgroundColor = RGBA(237, 237, 237, 1);
    [indicators addSubview:kaluLabel];
    kaluLabel.text = @"卡路里";
    kaluLabel.layer.borderWidth = 1;
    kaluLabel.textAlignment = NSTextAlignmentCenter;
    [kaluLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(speedLabel.mas_right);
        make.size.mas_equalTo(speedLabel);
        make.top.mas_equalTo(speedLabel);
        
    }];
    
    //时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.backgroundColor = RGBA(237, 237, 237, 1);
    [indicators addSubview:timeLabel];
    timeLabel.text = @"时间";
    timeLabel.layer.borderWidth = 1;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kaluLabel.mas_right);
        make.size.mas_equalTo(kaluLabel);
        make.top.mas_equalTo(kaluLabel);
        
    }];
    
    //显示速度
    self.speedNowLabel = [[UILabel alloc]init];
    self.speedNowLabel.backgroundColor = RGBA(250, 235, 215, 1);
    [indicators addSubview: self.speedNowLabel];
    self.speedNowLabel.text = @"等待数据";
    _speedNowLabel.layer.borderWidth = 1;
    self.speedNowLabel.textAlignment = NSTextAlignmentCenter;
    [ self.speedNowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(speedLabel);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(speedLabel);
        make.top.mas_equalTo(speedLabel.mas_bottom);
        
    }];
    
    //显示速度
    UILabel *speedSmallLabel = [[UILabel alloc]init];
    speedSmallLabel.backgroundColor = [UIColor clearColor];
    [indicators addSubview:speedSmallLabel];
    speedSmallLabel.text = @"km/h";
    speedSmallLabel.font = [UIFont systemFontOfSize:15];
    speedSmallLabel.textAlignment = NSTextAlignmentCenter;
    [speedSmallLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(speedLabel);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(40);
        make.top.mas_equalTo(speedLabel.mas_bottom).offset(13);
        
    }];
    
    
    //显示卡路里
    self.kaluNowLabel = [[UILabel alloc]init];
    self.kaluNowLabel.backgroundColor = RGBA(250, 235, 215, 1);
    [indicators addSubview:self.kaluNowLabel];
    self.kaluNowLabel.text = @"等待数据";
    _kaluNowLabel.layer.borderWidth = 1;
    self.kaluNowLabel.textAlignment = NSTextAlignmentCenter;
    [self.kaluNowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_speedNowLabel.mas_right);
        make.height.mas_equalTo(_speedNowLabel);
        make.width.mas_equalTo(_speedNowLabel);
        make.top.mas_equalTo(_speedNowLabel);
        
    }];
    
    //显示卡路里
    UILabel *kaluSmallLabel = [[UILabel alloc]init];
    kaluSmallLabel.backgroundColor = [UIColor clearColor];
    [indicators addSubview:kaluSmallLabel];
    kaluSmallLabel.text = @"kcal";
    kaluSmallLabel.font = [UIFont systemFontOfSize:15];
    kaluSmallLabel.textAlignment = NSTextAlignmentCenter;
    [kaluSmallLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_kaluNowLabel);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(30);
        make.top.mas_equalTo(speedLabel.mas_bottom).offset(13);
        
    }];
    
    //显示时间
    self.timeNowLabel = [[UILabel alloc]init];
    self.timeNowLabel.backgroundColor = RGBA(250, 235, 215, 1);
    [indicators addSubview:self.timeNowLabel];
    self.timeNowLabel.text = @"等待数据";
    _timeNowLabel.layer.borderWidth = 1;
    self.timeNowLabel.textAlignment = NSTextAlignmentCenter;
    [self.timeNowLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_kaluNowLabel.mas_right);
        make.height.mas_equalTo(_kaluNowLabel);
        make.width.mas_equalTo(_kaluNowLabel);
        make.top.mas_equalTo(_kaluNowLabel);
        
    }];
    
    
    
    //本次距离
    
    
    UILabel *longLabel = [[UILabel alloc]init];
    longLabel.backgroundColor = RGBA(237, 237, 237, 1);
    [indicators addSubview:longLabel];
    longLabel.text = @"本次运动距离:";
    longLabel.layer.borderWidth = 1;
    longLabel.font = [UIFont systemFontOfSize:13];
    longLabel.textAlignment = NSTextAlignmentCenter;
    [longLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_speedNowLabel);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(APPW / 4);
        make.top.mas_equalTo(_speedNowLabel.mas_bottom).offset(20);
        
    }];
    //本次距离数据
    self.longDataLabel = [[UILabel alloc]init];
    self.longDataLabel.backgroundColor = RGBA(250, 235, 215, 1);
    [indicators addSubview:self.longDataLabel];
    self.longDataLabel.text = @"运动数据";
    _longDataLabel.layer.borderWidth = 1;
    self.longDataLabel.font = [UIFont systemFontOfSize:17];
    [self.longDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(longLabel.mas_right);
        make.height.mas_equalTo(longLabel);
        make.width.mas_equalTo(longLabel);
        make.top.mas_equalTo(longLabel);
        
    }];
    
    
    //本月距离
    
    
    UILabel *longAllLabel = [[UILabel alloc]init];
    longAllLabel.backgroundColor = RGBA(237, 237, 237, 1);
    [indicators addSubview:longAllLabel];
    longAllLabel.text = @"本月运动距离:";
    longAllLabel.layer.borderWidth = 1;
    longAllLabel.font = [UIFont systemFontOfSize:13];
    longAllLabel.textAlignment = NSTextAlignmentCenter;
    [longAllLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_longDataLabel.mas_right);
        make.height.mas_equalTo(_longDataLabel);
        make.width.mas_equalTo(_longDataLabel);
        make.top.mas_equalTo(_longDataLabel);
        
    }];
    //本月距离数据
    self.longAllDataLabel = [[UILabel alloc]init];
    self.longAllDataLabel.backgroundColor = RGBA(250, 235, 215, 1);
    [indicators addSubview:self.longAllDataLabel];
    self.longAllDataLabel.text = @"运动数据";
    _longAllDataLabel.layer.borderWidth = 1;
    self.longAllDataLabel.font = [UIFont systemFontOfSize:17];
    [self.longAllDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(longAllLabel.mas_right);
        make.height.mas_equalTo(longAllLabel);
        make.width.mas_equalTo(longAllLabel);
        make.top.mas_equalTo(longAllLabel);
        
        
        
    }];
    
    
    
    starButton = [UIButton buttonWithType:UIButtonTypeCustom];
    starButton.backgroundColor = RGBA(156, 156, 156, 1);
    starButton.layer.cornerRadius=15;
    starButton.layer.masksToBounds=YES;
    [starButton setTitle:@"开始计步" forState:UIControlStateNormal];
    [starButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [starButton addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchUpInside];
    [indicators  addSubview:starButton];
    [starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(longLabel.mas_bottom).offset(75);
        make.left.equalTo(indicators).offset(20);
        make.right.equalTo(indicators).offset(-20);
        
        make.width.equalTo(indicators).multipliedBy(0.3);
        make.height.equalTo(@(55));
    }];
    
    
#pragma mark 开始跑步
    RunButton = [UIButton buttonWithType:UIButtonTypeCustom];
    RunButton.backgroundColor = RGBA(156, 156, 156, 1);
    RunButton.layer.cornerRadius=15;
    RunButton.layer.masksToBounds=YES;
    [RunButton setTitle:@"开始跑步" forState:UIControlStateNormal];
    [RunButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [RunButton addTarget:self action:@selector(RunButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [indicators  addSubview:RunButton];
    [RunButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(starButton);
        make.left.equalTo(starButton.mas_right).offset(100);
        //        make.right.equalTo(indicators).offset(-20);
        
        make.width.equalTo(starButton);
        make.height.equalTo(starButton);
    }];
    
    
    
    
    
    //当前日期
    NSDate *now = [NSDate date];
    //把当前的日期转化成字符串
    NSString *timeStr = [NSString stringWithFormat:@"%@",now];
    NSLog(@"%@", timeStr);
    //本月日期字符串
    self.month = [timeStr substringToIndex:7];
    NSLog(@"%@", self.month);
    
    
    
    if ([self.dataArr lastObject][@"month"] == nil) {
        self.speedNowLabel.text = @"0.0";
        self.kaluNowLabel.text = @"0.0";
        self.timeNowLabel.text = @"00.00";
        self.longDataLabel.text = @"0.0";
        self.longAllDataLabel.text =@"0.0";
        
        
    }else{
        self.speedNowLabel.text = @"0.0";
        self.kaluNowLabel.text = @"0.0";
        self.timeNowLabel.text = @"00:00:00";
        self.longDataLabel.text = @"0.0";
        
        self.a = [self.dataArr lastObject][@"distance"];
//        NSLog(@"路程***********%@",[self.dataArr lastObject][@"distance"]);
        
        CGFloat f = 0;
        for (NSMutableDictionary *dic in self.dataArr) {
            f = (f + [dic[@"distance"] doubleValue]);
        }
        //运动距离
        self.longAllDataLabel.text = [NSString stringWithFormat:@"%.2f", f / 1000];
     }
   
}





#pragma mark 开始按钮

-(void)starClick:(UIButton *)button{
    if (button.selected == NO) {
        
        NSLog(@"开始计步");
        
        //判断计步器是否好用
        if (![CMPedometer isStepCountingAvailable]){
            [self AlertController:@"提示" withMessage:@"该功能仅支持5s以后版本"];
            return;
        }
        [button setTitle:@"停止" forState: UIControlStateNormal];
        
        //创建计时器对象
        self.Pedometer = [[CMPedometer alloc]init];
        //开始计步  [NSDate date] 当时时间
        [self.Pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
            if (error) {
                NSLog(@"开始计步出现错误 ==== %@",error);
                return;
            }
            //获取走了多少步
            NSString *str1 = [NSString stringWithFormat:@"%@",pedometerData.numberOfSteps];
            i = [str1 intValue];
            v = i+v;
            view1.num = v;
            
             view1.numLabel.text = str1;
            NSLog(@"---------------%d",v);
            
        }];
        
        NSLog(@"-----++++----------%d",v);
        
    } else {
        [button setTitle:@"开始计步" forState: UIControlStateNormal];
        k  = v*0.05/1000;
        //   footlaber.text = [NSString stringWithFormat:@"您走了%.2f公里",k];
        //停止计步
        [_Pedometer stopPedometerUpdates];
        
        NSString *step1 = [NSString stringWithFormat:@"%d",v];
        NSString *string = [NSString stringWithFormat:@"您走了%.2f公里,消耗了%d卡路里",k,v];
        
        [self AlertController:@"温馨提示" withMessage:string];
        
        NSLog(@"停止计步啊啊------%@",step1);
        
    }
    button.selected = !button.selected;
    
    
}




#pragma mark 开始跑步方法
-(void)RunButtonClick:(UIButton *)button{
    // 创建CLLocationManager对象
    //self.CLLMan = [[CLLocationManager alloc] init];
    
    
    if (button.selected == NO) {
        [button setTitle:@"停止跑步" forState: UIControlStateNormal];
        
        if ([CLLocationManager locationServicesEnabled]) {
            
            //1.创建位置管理器（定位用户的位置）
            _CLLMan=[[CLLocationManager alloc]init];
            //2.设置代理
            self.CLLMan.delegate=self;
            
            
            // 设置定位精度：最佳精度
            self.CLLMan.desiredAccuracy = kCLLocationAccuracyBest;
            // 设置距离过滤器为50米，表示每移动50米更新一次位置(设置为任何移动)
            self.CLLMan.distanceFilter = kCLDistanceFilterNone;
            // 将视图控制器自身设置为CLLocationManager的delegate
            // 因此该视图控制器需要实现CLLocationManagerDelegate协议
            //            self.CLLMan.delegate = self;
            if ([[[UIDevice currentDevice] systemVersion] doubleValue]>8.0) {
                [_CLLMan requestAlwaysAuthorization];
            }
            
            [self RunTime];
            NSLog(@"定位成功");
        }else{
            
            [self AlertController:@"提示" withMessage:@"定位不可用"];
            
        }
        
        // 开始监听定位信息
        [self.CLLMan startUpdatingLocation];
        
        
    }else{
        [button setTitle:@"开始跑步" forState: UIControlStateNormal];
        
        [self.CLLMan stopUpdatingLocation];
        [self.timer invalidate];
        [[DataBaseManeger sharaInstance] insertDistance:self.sum Time:self.Sum_counts Data:self.month];
        
    }
    
    button.selected = !button.selected;
    
    
    
    
}


#pragma mark GPS定位
//#if 0
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    
//    [[DataBaseManeger sharaInstance] openDB];
//    self.dataArr = [[DataBaseManeger sharaInstance] selectAll];
//#pragma mark 实时速度
//    
//    //当前日期
//    NSDate *now = [NSDate date];
//    //把当前的日期转化成字符串
//    NSString *timeStr = [NSString stringWithFormat:@"%@",now];
//    NSLog(@".........%@", timeStr);
//    //本月日期字符串
//    self.month = [timeStr substringToIndex:7];
//    NSLog(@".......%@", self.month);
//    NSLog(@".....%@",[self.dataArr lastObject][@"month"]);
//    if (![[self.dataArr lastObject][@"month"] isEqualToString:_month]) {
//        NSFileManager *fileManager = [NSFileManager defaultManager];
//        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        NSString *dbPath = [path stringByAppendingPathComponent:@"car.db"];
//        [fileManager removeItemAtPath:dbPath error:nil];
//        [[DataBaseManeger sharaInstance] openDB];
//        [[DataBaseManeger sharaInstance] createTable];
//        
//        
//    }
//    
//    self.a = [self.dataArr lastObject][@"distance"];
//    //    self.b = [self.dataArr lastObject][@"count"];
//    NSLog(@"路程***********%@",[self.dataArr lastObject][@"distance"]);
//    
//    //       self.longDataLabel.text = [NSString stringWithFormat:@"%@",self.a];
//    //运动次数
//    //self.sportCounts.text = [NSString stringWithFormat:@"%ld", (unsigned long)self.dataArr.count];
//    //    NSLog(@"%@", [self.dataArr lastObject][@"count"]);
//    //    NSLog(@"%@", self.b);
//    //
//    CGFloat f = 0;
//    for (NSMutableDictionary *dic in self.dataArr) {
//        f = (f + [dic[@"distance"] doubleValue]);
//    }
//    //运动距离
//    self.longDataLabel.text = [NSString stringWithFormat:@"%.2f", f / 1000];
//    
//    // [self.runItemTableView reloadData];
//    
//}
//#endif
#pragma mark 跑步路程
-(void)runingLong{
    
    
    self.dataArr = [[DataBaseManeger sharaInstance] selectAll];
    
    
    //当前日期
    NSDate *now = [NSDate date];
    //把当前的日期转化成字符串
    NSString *timeStr = [NSString stringWithFormat:@"%@",now];
    NSLog(@".........%@", timeStr);
    //本月日期字符串
    self.month = [timeStr substringToIndex:7];
    NSLog(@".......%@", self.month);
    NSLog(@".....%@",[self.dataArr lastObject][@"month"]);
    self.a = [self.dataArr lastObject][@"distance"];
    //    self.b = [self.dataArr lastObject][@"count"];
    NSLog(@"路程***********%@",[self.dataArr lastObject][@"distance"]);
    
    //       self.longDataLabel.text = [NSString stringWithFormat:@"%@",self.a];
    //运动次数
    //self.sportCounts.text = [NSString stringWithFormat:@"%ld", (unsigned long)self.dataArr.count];
    //    NSLog(@"%@", [self.dataArr lastObject][@"count"]);
    //    NSLog(@"%@", self.b);
    //
    CGFloat f = 0;
    for (NSMutableDictionary *dic in self.dataArr) {
        f = (f + [dic[@"distance"] doubleValue]);
    }
    //本月运动距离
    self.longAllDataLabel.text = [NSString stringWithFormat:@"%.2f", f / 1000];
    
    
    
    
}




#pragma mark 更新定位
#pragma mark 实时速度

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    NSLog(@"更新定位");
    
    CLLocation *newLocation = [locations lastObject];
    
    if (self.oldLocation) {
        NSTimeInterval dTime = [newLocation.timestamp timeIntervalSinceDate:self.oldLocation.timestamp];
        self.sumTime += dTime;
        
        CGFloat dis = [newLocation distanceFromLocation:self.oldLocation];
        if (dis < 1.0f) {
            return;
        }
        
        self.sum += dis;
        //路程
        self.longDataLabel.text = [NSString stringWithFormat:@"%.2f",self.sum / 1000];
        //实时速度
        self.speedNowLabel.text = [NSString stringWithFormat:@"%.2f", newLocation.speed + 1];
        
        NSLog(@"速度%@",_speedNowLabel.text);
        CGFloat kcal = ((newLocation.speed + 1) * 60 * 60 / 1000) * 70 * 1.5 / 60 / 60;
        
        self.sumkcal = self.sumkcal + kcal;
        NSLog(@"消耗的卡路里%f",self.sumkcal);
        
        self.kaluNowLabel.text = [NSString stringWithFormat:@"%.1f",self.sumkcal];
    }
    self.oldLocation = newLocation;
    
    
    
    
}
#pragma mark 跑步时间
-(void)RunTime{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UserTimer) userInfo:nil repeats:YES];
    [self.timer setFireDate:[NSDate distantPast]];
    
}

-(void)UserTimer{
    
    self.time_sec++;
    if (self.time_sec == 60) {
        self.time_min++;
        self.time_sec = 0;
        if (self.time_min == 60) {
            self.time_hour++;
            self.time_min = 0 ;
            self.time_sec = 0;
        }
    }
    
    self.timeNowLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)self.time_hour,(long)self.time_min,(long)self.time_sec];//保留两位 不够补零
    //    NSLog(@"%ld",(long)self.time_sec);
    //    NSLog(@"%ld",(long)self.time_min);
    
    
}



// 定位失败时激发的方法
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    [self AlertController:@"提示" withMessage:@"定位失败"];
}

#pragma mark 用户决定
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
            // 用户还未决定
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"用户还未决定");
            break;
        }
            // 问受限
        case kCLAuthorizationStatusRestricted:
        {
            NSLog(@"访问受限");
            break;
        }
            // 定位关闭时和对此APP授权为never时调用
        case kCLAuthorizationStatusDenied:
        {
            // 定位是否可用（是否支持定位或者定位是否开启）
            if([CLLocationManager locationServicesEnabled])
            {
                NSLog(@"定位开启，但被拒");
            }else
            {
                NSLog(@"定位关闭，不可用");
            }
            //            NSLog(@"被拒");
            break;
        }
            // 获取前后台定位授权
        case kCLAuthorizationStatusAuthorizedAlways:
            //        case kCLAuthorizationStatusAuthorized: // 失效，不建议使用
        {
            NSLog(@"获取前后台定位授权");
            break;
        }
            // 获得前台定位授权
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            NSLog(@"获得前台定位授权");
            break;
        }
        default:
            break;
    }
    
    
    
}

#pragma mark 点击backbutton返回
-(void)buttonAction:(UIButton *)button{


[self dismissViewControllerAnimated:YES completion:^{
    
    
}];


}

#pragma mark AlertController封装

-(void)AlertController:(NSString *)title withMessage:(NSString *)message{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:^{
        
        
    }];
    
    
}



@end
